package com.hp.bon.sgw.start;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hp.bon.sgw.bean.CPUOverloadBean;
import com.hp.bon.sgw.bean.CongestionBean;
import com.hp.bon.sgw.bean.SysPertriNetBean;
import com.hp.bon.sgw.service.ConfigService;
import com.hp.bon.sgw.service.DataStatusService;
import com.hp.bon.sgw.service.OuterDBDataService;
import com.hp.bon.sgw.service.SGWDataStoreService;

public class SpringContext {

	private static SpringContext instance;

	public static SpringContext getInstance() {
		if (instance == null) {
			instance = new SpringContext();
		}
		return instance;
	}

	ConfigService configService;
	SGWDataStoreService dataStoreService;
	DataStatusService dataStatusService;
	CongestionBean congestionBean;
	OuterDBDataService outerDBDataService;
	CPUOverloadBean cpuOverloadBean;

	private boolean isInit = false;
	private static ClassPathXmlApplicationContext ctx = null;

	public static ClassPathXmlApplicationContext getApplicationContext() {
		return ctx;
	}

	public void init() {
		if (!isInit) {
			String[] springXmls = new String[] { "spring-base.xml", "spring-sgwserver.xml" };
			ctx = new ClassPathXmlApplicationContext(springXmls);
			ctx.start();
			// startTJWS();
			// SysCtrlService sysCtrlService =
			// ctx.getBean(SysCtrlService.class);
			// sysCtrlService.doPeriodJob();

			Thread shutdownHook = new Thread() {
				public void run() {
					System.out.println("MyShutDown thread started");
					SpringContext.this.stop();
					System.out.println("MyShutDown thread finsihed");
				}
			};
			Runtime.getRuntime().addShutdownHook(shutdownHook);
			isInit = true;
		}
	}

	public void stop() {
		try {
			ctx.stop();
		} finally {
			ctx.close();
			ctx = null;
		}

	}

	public ConfigService getConfigService() {
		if (configService == null) {
			configService = ctx.getBean(ConfigService.class);
		}
		return configService;
	}

	public SGWDataStoreService getDataStoreService() {
		if (dataStoreService == null) {
			dataStoreService = ctx.getBean(SGWDataStoreService.class);
		}
		return dataStoreService;
	}

	public DataStatusService getDataStatusService() {
		if (this.dataStatusService == null) {
			this.dataStatusService = ((DataStatusService) ctx.getBean(DataStatusService.class));
		}
		return this.dataStatusService;
	}

	public CongestionBean getCongestionBean() {
		if (congestionBean == null) {
			congestionBean = ctx.getBean(CongestionBean.class);
		}
		return congestionBean;
	}

	public CPUOverloadBean getCPUOverloadBean() {
		if (cpuOverloadBean == null) {
			cpuOverloadBean = ctx.getBean(CPUOverloadBean.class);
		}
		return cpuOverloadBean;
	}

	SysPertriNetBean pertriBean = null;

	public SysPertriNetBean getPertriBean() {
		if (pertriBean == null) {
			pertriBean = ctx.getBean(SysPertriNetBean.class);
		}
		return pertriBean;
	}

	public OuterDBDataService getOuterDBDataService() {
		if (outerDBDataService == null) {
			outerDBDataService = ctx.getBean(OuterDBDataService.class);
		}
		return outerDBDataService;
	}

	public static void setCtx(ClassPathXmlApplicationContext ctx) {
		SpringContext.ctx = ctx;
	}
}